class Solution(object):
    def largestEvenSum(self, nums, k):
        d = [[] for _ in range(2)]
        for num in nums:
            d[num % 2].append(num)
        for i in range(2):
            d[i].sort(reverse=True)
        for i in range(2):
            for j in range(1, len(d[i])):
                d[i][j] += d[i][j - 1]
        max_value = -1
        for t in range(0, len(d[1]) + 1, 2):
            if 0 <= k - t <= len(d[0]):
                if k - t == 0:
                    t1 = 0
                else:
                    t1 = d[0][k - t - 1]
                if t == 0:
                    t2 = 0
                else:
                    t2 = d[1][t - 1]
                max_value = max(max_value, t1 + t2)
        return max_value